package com.example.homework;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class FileMainActivity extends AppCompatActivity {

    Button button1, button2, button3, button4;
    TextView textView1;

    MyDAO myDAO;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_main);

        textView1 = findViewById(R.id.textView_file_1);
        button1 = findViewById(R.id.button_file_1);
        button2 = findViewById(R.id.button_file_2);
        button3 = findViewById(R.id.button_file_3);
        button4 = findViewById(R.id.button_file_4);

        myDAO = new MyDAO(this);

        // 初始化数据
        myDAO.resetTable();
        myDAO.insert("张三", 20);
        myDAO.insert("李四", 22);
        myDAO.insert("王五", 25);

        // 增加数据
        button1.setOnClickListener(v -> {
            myDAO.insert("赵六", 30);
            textView1.setText("插入数据成功：赵六, 30");
        });

        // 查询数据
        button2.setOnClickListener(v -> {
            String result = myDAO.queryAll();
            textView1.setText("查询结果：\n" + result);
        });

        // 更新数据
        button3.setOnClickListener(v -> {
            myDAO.update("张三", 28);
            textView1.setText("更新数据成功：张三 -> 28岁");
        });

        // 删除数据
        button4.setOnClickListener(v -> {
            myDAO.delete("李四");
            textView1.setText("删除数据成功：李四");
        });
    }
}
